export const indexedDBStorage = {
    async getItem(key) {
      return new Promise((resolve) => {
        const request = indexedDB.open('PiniaStoreDB', 1)
        
        request.onupgradeneeded = (event) => {
          const db = event.target.result
          if (!db.objectStoreNames.contains('PiniaStore')) {
            db.createObjectStore('PiniaStore')
          }
        }
        
        request.onsuccess = (event) => {
          const db = event.target.result
          const transaction = db.transaction('PiniaStore', 'readonly')
          const store = transaction.objectStore('PiniaStore')
          const getRequest = store.get(key)
          
          getRequest.onsuccess = () => {
            resolve(getRequest.result || null)
          }
          
          getRequest.onerror = () => {
            resolve(null)
          }
        }
        
        request.onerror = () => {
          resolve(null)
        }
      })
    },
    
    async setItem(key, value) {
      return new Promise((resolve) => {
        const request = indexedDB.open('PiniaStoreDB', 1)
        
        request.onupgradeneeded = (event) => {
          const db = event.target.result
          if (!db.objectStoreNames.contains('PiniaStore')) {
            db.createObjectStore('PiniaStore')
          }
        }
        
        request.onsuccess = (event) => {
          const db = event.target.result
          const transaction = db.transaction('PiniaStore', 'readwrite')
          const store = transaction.objectStore('PiniaStore')
          const putRequest = store.put(value, key)
          
          putRequest.onsuccess = () => {
            resolve()
          }
          
          putRequest.onerror = () => {
            resolve()
          }
        }
        
        request.onerror = () => {
          resolve()
        }
      })
    },
    
    async removeItem(key) {
      return new Promise((resolve) => {
        const request = indexedDB.open('PiniaStoreDB', 1)
        
        request.onsuccess = (event) => {
          const db = event.target.result
          const transaction = db.transaction('PiniaStore', 'readwrite')
          const store = transaction.objectStore('PiniaStore')
          const deleteRequest = store.delete(key)
          
          deleteRequest.onsuccess = () => {
            resolve()
          }
          
          deleteRequest.onerror = () => {
            resolve()
          }
        }
        
        request.onerror = () => {
          resolve()
        }
      })
    }
  }